package com.example.canal.listener;

import com.example.canal.app.DataSyncApp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;


@Component
public class DataSyncContextListener implements ApplicationListener<ContextRefreshedEvent> {

    private static final Logger LOGGER = LoggerFactory.getLogger(DataSyncContextListener.class);

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        DataSyncApp syncApp = applicationContext.getBean(DataSyncApp.class);
        LOGGER.info("开始运行数据同步任务...");
        threadPoolTaskExecutor.submit(new Runnable() {
            @Override
            public void run() {
                syncApp.work();
            }
        });
    }
}
